<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!--          <el-form-item label="编号" prop="id">-->
          <!--            <el-input v-model="form.id" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="省份">-->
          <!--            <el-input v-model="form.provinceId" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <el-form-item label="系统">
            <el-select v-model="form.systemId" filterable placeholder="请选择" @change="proviceGet(form.systemId)">
              <el-option
                v-for="item in system"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="false" label="系统名">
            <el-input v-model="form.systemName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="省份">
            <el-select v-model="form.provinceId" filterable placeholder="请选择" @change="cityGet(form.provinceId)">
              <el-option
                v-for="item in provice"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="市">
            <el-select v-model="form.cityId" filterable placeholder="请选择" @change="areaGet(form.cityId)">
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="县区">
            <el-select v-model="form.areaId" filterable placeholder="请选择">
              <el-option
                v-for="item in area"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="市县区">-->
          <!--            <el-input v-model="form.cityId" style="width: 370px;"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="岗位名称">-->
          <!--            <el-input v-model="form.jobId" style="width: 370px;"/>-->
          <!--          </el-form-item>-->
          <el-form-item label="岗位名称">
            <el-select v-model="form.jobId" filterable placeholder="请选择">
              <el-option
                v-for="item in job"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="分成比例">
            <el-input v-model="form.percent" style="width: 370px;" />
          </el-form-item>
          <el-form-item label=" 是否有效">
            <el-select v-model="form.status" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.integral_rule_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="创建时间">-->
          <!--            <el-input v-model="form.createTime" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="编号" />
        <el-table-column v-if="false" prop="systemId" label="系统" />
        <el-table-column prop="systemName" label="系统" />
        <el-table-column v-if="false" prop="provinceId" label="省份" />
        <el-table-column prop="provinceName" label="省份" />
        <el-table-column v-if="false" prop="cityId" label="市" />
        <el-table-column prop="cityName" label="市" />
        <el-table-column v-if="false" prop="areaId" label="县区" />
        <el-table-column prop="areaName" label="县区" />
        <el-table-column v-if="false" prop="jobId" label="岗位名称" />
        <el-table-column prop="jobName" label="岗位名称" />
        <el-table-column prop="percent" label="分成比例" />
        <el-table-column prop="status" label=" 是否有效">
          <template slot-scope="scope">
            {{ dict.label.integral_rule_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column
          v-permission="['admin','integralRule:edit','integralRule:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudIntegralRule from '@/api/integral/integralRule'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudDept from '@/api/system/dept'
import crudJob from '@/api/system/job'

// eslint-disable-next-line no-unused-vars
const defaultForm = {
  id: null,
  systemId: null,
  systemName: null,
  provinceId: null,
  provinceName: null,
  cityId: null,
  cityName: null,
  areaId: null,
  areaName: null,
  jobId: null,
  percent: null,
  status: null,
  createTime: null,
  remark: null,
  jobName: null
}
export default {
  name: 'IntegralRule',
  // eslint-disable-next-line vue/no-unused-components
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['integral_rule_status'],
  cruds() {
    return CRUD({
      title: '分润配置',
      url: 'api/integralRule',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudIntegralRule }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'integralRule:add'],
        edit: ['admin', 'integralRule:edit'],
        del: ['admin', 'integralRule:del']
      },
      rules: {
        id: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ]
      },
      system: '',
      provice: '',
      city: '',
      area: '',
      job: ''
    }
  },
  mounted() {
    this.systemGet()
    this.jobGet()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      form.status = form.status.toString()
    },
    systemGet() {
      // 获取系统下拉框数据
      let pro = {}
      const that = this
      setTimeout(() => {
        crudDept.getDepts({ pidIsNull: true }).then(res => {
          pro = res
          that.system = res.content
        })
      }, 100)
    },
    proviceGet(value) {
      // 获取不同系统下省份拉框数据
      let pro = {}
      const that = this
      setTimeout(() => {
        crudDept.getDepts({ pid: value }).then(res => {
          pro = res
          that.provice = res.content
        })
      }, 100)
    },
    cityGet(value) {
      let pro = {}
      const that = this
      setTimeout(() => {
        crudDept.getDepts({ pid: value }).then(res => {
          pro = res
          that.city = res.content
        })
      }, 100)
    },
    areaGet(value) {
      let pro = {}
      const that = this
      setTimeout(() => {
        crudDept.getDepts({ pid: value }).then(res => {
          pro = res
          that.area = res.content
        })
      }, 100)
    },
    jobGet() {
      let pro = {}
      const that = this
      setTimeout(() => {
        crudJob.getAllJob().then(res => {
          pro = res
          that.job = res.content
        })
      }, 100)
    }
  }
}
</script>

<style scoped>

</style>
